#! /bin/bash
# o programa recebe 4 parametros ( as coordenadas dos dois centroids iniciais)
# @1 - c1
# @2 - c2


c1=$1
c2=$2


cluster1=""
cluster2=""
distancia1=""
distancia2=""
contrib1=0
n1=0
contrib2=0
n2=0


FS=,
while read x  
do
	dist1=$(echo " r=$x - $c1; if (0 > r) r= r * -1; r"  | bc)
	dist2=$(echo " r=$x - $c2; if (0 > r) r= r * -1; r"  | bc)

	distancia1="$distancia1 $dist1 "
	distancia2="$distancia2 $dist2 "

	
	result=$(echo " scale=2; r = 2;  if($dist2 >= $dist1) r=1; r" | bc)
	if [ "$result" -eq 1 ] 
	then
		cluster1="$cluster1 1 "
		cluster2="$cluster2 0 "
		contrib1=$( echo "scale=2; $contrib1 + $x" | bc)
		n1=$((n1 + 1))
	else
		cluster1="$cluster1 0 "
		cluster2="$cluster2 1 "
		contrib2=$( echo "scale=2; $contrib2 + $x" | bc)
		n2=$((n2 + 1))
	fi 

done < tab5

echo "centroids iniciais"
echo $1 
echo $2 

echo "distancias:"
echo $distancia1
echo $distancia2


echo "Clusters:"
echo $cluster1
echo $cluster2

echo "Novos Centroids:"
echo C1: $(echo "scale=2; $contrib1 / $n1 " | bc) 
echo C2: $(echo "scale=2; $contrib2 / $n2 " | bc)
 
